package com.xc.hotelmgr.order.modules.ctrip.config;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xc.hotelmgr.order.common.utils.RedisUtils;
import com.xc.hotelmgr.order.modules.ctrip.service.MonitOrdStatusService;
import com.xc.hotelmgr.order.modules.order.entity.DtOrderChannelsEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
public class MultithreadScheduleTask {
    @Autowired
    private MonitOrdStatusService monitOrdStatusService;

    @Autowired
    private RedisUtils redisUtils;

//    @Scheduled(fixedDelay = 3000)  //间隔5秒
    @Scheduled(cron = "${platform.parameters.scheduling:-}")
    public void monitor() throws InterruptedException {
        String ctripOrderChannels = redisUtils.get("ctripOrderChannels");
        if(StringUtils.isBlank(ctripOrderChannels)){
            return;
        }
        List<DtOrderChannelsEntity> ctrips = JSONObject.parseObject(ctripOrderChannels, new TypeReference<List<DtOrderChannelsEntity>>(){});
        DtOrderChannelsEntity dtOrderChannelsEntity = null;
        if(ctrips == null){
            return;
        }
        for (int index = 0; index < ctrips.size();index ++) {
            dtOrderChannelsEntity = ctrips.get(index);
            monitOrdStatusService.monitOrdStatusAsyncTask(dtOrderChannelsEntity);
        }
    }

}